#!/usr/bin/ruby
 
load 'deploy'
 
set :application, "ota"
 
default_run_options[:pty] = true
 
# DEPLOYMENT SCHEME
set :scm, :none
set :deploy_via, :copy
set :repository do 
  fetch(:deploy_from)
end
 
# LOCAL
set :war, "target/ota.war"
 
# TOMCAT SERVERS
role :webserver, "10.200.107.34"
set :tomcat_home, "/usr/local/tomcat/"
set :tomcat_ctrl, "/usr/local/tomcat/bin/"
 
# USER / SHELL
set :user, "root" # the user to run remote commands as
#set :use_sudo, false
set :use_sudo, true
 
set :deploy_from do 
  dir = "/tmp/prep_#{release_name}"
  system("mkdir -p #{dir}")
  dir
end
 
# this is capistrano's default location.
# depending on the permissions of the server
# you may need to create it and chown it over
# to :user (e.g. chown -R robotuser:robotuser /u)
set :deploy_to do 
  "/u/apps/#{application}"
end
 
#
# simple interactions with the tomcat server
#
namespace :tomcat do
 
  desc "start tomcat"
  task :start do
	cmd = "nohup sh #{tomcat_ctrl}startup.sh"
	puts cmd
	sudo cmd 
  end
 
  desc "stop tomcat"
  task :stop do
	cmd = "sh #{tomcat_ctrl}shutdown.sh"
  	puts cmd
	sudo cmd
  end
 
  desc "stop and start tomcat"
  task :restart do
    tomcat.stop
    tomcat.start
  end
 
  desc "tail :tomcat_home/logs/*.log and logs/catalina.out"
  task :tail do
    stream "tail -f #{tomcat_home}/logs/*.log #{tomcat_home}/logs/catalina.out"
  end
 
end
 
#
# link the current/whatever.war into our webapps/whatever.war
#
after 'deploy:setup' do
  cmd = "ln -s #{deploy_to}/current/`basename #{war}` #{tomcat_home}/webapps/`basename #{war}`"
  puts cmd
  sudo cmd
end
 
# collect up our war into the deploy_from folder
# notice that all we're doing is a copy here,
# so it is pretty easy to swap this out for
# a wget command, which makes sense if you're
# using a continuous integration server like
# bamboo. (more on this later).

namespace :build do
	
	desc "update code from svn"
	task :update do
  		puts "Updating code from subversion..."
  		successed = system("svn up")
		raise RuntimeError.new("Failed to update code!") unless successed
	end

	desc "war file with maven"
	task :warfile do
  		cmd = "mvn clean package -P product"
  		puts cmd
  		successed = system("#{cmd}")	
		raise RuntimeError.new("Failed to build war!") unless successed
	end

end  


before 'deploy:update_code' do
  
  build.update 
  build.warfile
	
  unless(war.nil?)
    puts "get war"
    system("cp #{war} #{deploy_from}")
    puts system("ls -l #{deploy_from}")
  end
end
 
# restart tomcat
namespace :deploy do
  task :restart do
    tomcat.restart
  end
end
 
#
# Disable all the default tasks that
# either don't apply, or I haven't made work.
#
namespace :deploy do
  [ :upload, :cold, :start, :stop, :migrate, :migrations ].each do |default_task|
    desc "[internal] disabled"
    task default_task do
      # disabled
    end
  end
 
  namespace :web do
    [ :disable, :enable ].each do |default_task|
      desc "[internal] disabled"
      task default_task do
        # disabled
      end
    end
  end
 
  namespace :pending do
    [ :default, :diff ].each do |default_task|
      desc "[internal] disabled"
      task default_task do
        # disabled
      end
    end
  end
end
 
